package com.mango.wall;

import android.app.Activity;
import android.app.Application;
import android.content.Intent;

import com.mango.config.BaseWall;
import com.mango.request.ReqMgUserLogin;
import com.mango.request.ReqMgWallExchange;
import com.mango.request.beans.UserLoginBean;
import com.mango.volley.toolbox.RequestFuture;
import com.mango.wall.activities.WallActivity;

import java.util.AbstractMap;

public final class MGW {
    public static String getVersion() {
        return BuildConfig.VERSION;
    }

    public static void init(Application application, String appID, String appKey) {
        BaseWall.getInstance().init(application, appID, appKey);
    }

    public static double getIntegralSync(String userID) {
        RequestFuture<UserLoginBean> future = RequestFuture.newFuture();
        BaseWall.getInstance().getQueue().add(new ReqMgUserLogin(BaseWall.getInstance()
                .getAppID(), BaseWall.getInstance().getAppKey(), userID, future, future));

        try {
            return future.get().user.integral;
        } catch (Exception e) {
            return -1;
        }
    }

    public static AbstractMap.SimpleEntry<Integer, String> exchangeSync(String userID, double integral) {
        RequestFuture<AbstractMap.SimpleEntry<Integer, String>> future = RequestFuture.newFuture();
        BaseWall.getInstance().getQueue().add(new ReqMgWallExchange(BaseWall.getInstance()
                .getAppID(), BaseWall.getInstance().getAppKey(), userID, integral, future, future));

        try {
            return future.get();
        } catch (Exception e) {
            return null;
        }
    }

    public static void gotoWall(Activity activity, int mgPosID, String userID) {
        Intent intent = new Intent(activity, WallActivity.class);
        intent.putExtra("posID", mgPosID);
        intent.putExtra("userID", userID);
        activity.startActivity(intent);
    }
}
